package com.bawei.a2010douyin;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.tencent.live2.V2TXLiveDef;
import com.tencent.live2.V2TXLivePusher;
import com.tencent.live2.impl.V2TXLivePusherImpl;
import com.tencent.rtmp.ui.TXCloudVideoView;

/**
 * 推流和拉流
 */
public class TencentActivity extends AppCompatActivity {
    private V2TXLivePusher mLivePusher;//pusher对象
    private TXCloudVideoView mPusherView;//摄像头预览view
    private StandardGSYVideoPlayer gsy;//拉流view



    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tencent);
        requestPermissions(new String[]{
                "android.permission.VIBRATE",
                "android.permission.RECORD_AUDIO" ,
                "android.permission.CAMERA",
                "android.permission.ACCESS_NETWORK_STATE",
                "android.permission.WRITE_EXTERNAL_STORAGE" ,
                "android.permission.ACCESS_FINE_LOCATION",
                "android.permission.GET_TASKS" ,
                "android.permission.ACCESS_WIFI_STATE" ,
                "android.permission.CHANGE_WIFI_STATE" ,
                "android.permission.WAKE_LOCK",
                "android.permission.MODIFY_AUDIO_SETTINGS",
                "android.permission.READ_PHONE_STATE",
                "android.permission.RECEIVE_BOOT_COMPLETED",
                "android.permission.FOREGROUND_SERVICE",
                "android.permission.ACCESS_COARSE_LOCATION"

        },101);


        gsy = (StandardGSYVideoPlayer) findViewById(R.id.gsy);
        // 指定对应的直播协议为 RTMP，该协议不支持连麦
        mLivePusher = new V2TXLivePusherImpl(this, V2TXLiveDef.V2TXLiveMode.TXLiveMode_RTMP);
// 启动本地摄像头预览
        mPusherView = (TXCloudVideoView) findViewById(R.id.pusher_tx_cloud_view);
        mLivePusher.setRenderView(mPusherView);
        mLivePusher.startCamera(true);
        mLivePusher.startMicrophone();


    }
    //开始推流
    public void startPush(View view) {
// 根据推流协议传入对应的 URL 即可启动推流， RTMP 协议以 rtmp:// 开头，该协议不支持连麦
        String url = "rtmp://3891.livepush.myqcloud.com/live/3891_user_2e3d101a_9e6e?bizid=3891&txSecret=813162506ae1e02923100fc12450ee0a&txTime=6433B544";
        int ret = mLivePusher.startPush(url);
        if (ret == -1) {
            Log.i("TAG", "startRTMPPush: license 校验失败");
        }else{
            Toast.makeText(this, "推流成功", Toast.LENGTH_SHORT).show();
        }

    }
    //结束推流
    public void stopPush(View view) {
        if(mLivePusher != null){
            // 结束推流
            mLivePusher.stopPush();
        }
    }
    //拉流
    public void play(View view) {
        gsy.setUp("https://0b7aadf7e79605e314a209692a38aaec.livehwc3.cn/hw-adaptive.pull.yximgs.com/gifshow/kwai_actL_ol_act_10873484433_strL_hd2000.flv?kabr_spts=-5000&cdn_redirect=true&tsc=origin&oidc=watchmen&sidc=5907&fd=1&srcStrm=G39bU8u_wsg&hwTime=6435070f&hwSecret=cc34fde0adb817e2eb13129c87584b56&cdn_redirect_tag=1681110416&policy=5&cdn_redirect_domain=657e385659c7310e89c5032cf12f394d.livehwc3.cn&ss=s20",true,"标题");
        gsy.startPlayLogic();
    }
}